<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Previous_event extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('previous_event_model', 'event');
		$this->load->model('contact_model');	
		$this->load->model('company_model');
		/*
		$this->load->model('contact_model');	
		$this->load->model('company_model');	
		$this->load->model('titles_model');	
		*/ 
	}
	
	public function index()
	{
		$data['msg'] 	= NULL;
		$this->load->view('previous_event/main', $data);
	}
	
	function get_info($id=NULL)
	{
		if(!$id)
		{
			$list = $this->event->find_all();
			$group = array();
			//$data['num_row'] = $list->num_rows();

			foreach($list as $row){
				$group[mb_substr($row->event_name,0,1,'utf-8')][] = array("id" => $row->id, "name" => $row->event_name);
			}
			
			$data['list']=$group;
		}
		
		if($id)
		{
			//sleep(1);
			$data['list']  = $this->event->find($id);
			$this->log('Previous Event', 'View record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
			//print_r($this->db->last_query());
			//die;
			
		}

		die(json_encode($data['list']));
		
	}
			
	function form($id=NULL)
	{
		//sleep(1);
		$data['event'] 		= $this->event->find_all();
		$data['company']  	= $this->company_model->where('deleted',0)->find_all();
		$data['contact']  	= $this->contact_model->where('deleted',0)->find_all();
		
		if(!empty($id))
			$data['event'] = $this->event->find($id);
			
		$this->load->view('previous_event/form', $data);
	}
	
	function delete($id=NULL)
	{
		if (!empty($id))
		{	
			//Must check if the event has no attached contact in contacts table
			
				if ($this->event->delete($id))
				{
					//success
						$data['msg']	= 'Event Successfully Deleted..';
						$data['error']	= false;
						$this->log('Previous Event', 'Delete record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
					}
				else{
					//failed to delete
					$data['error']	= true;
					$data['msg']	= ' Failed to Delete..';
				}
		die(json_encode($data));
		}
		
	}
		
		
	function create($form)
	{
		if($inserted_id = $this->event->insert($form))
		{
			$data['error']= false;
			$data['id']= $inserted_id;
			$data['msg']  = '<span style="color:green">New Event :<b>"'.$form['event_name'].'"</b> Has Been Added Successfully<br>Thanks You !</span>';
		}
		else{
			$data['error']= true;
			$data['msg']  = 'Data Base Error <br> ERROR: prvEvn/ins/100';
		}
			
		return $data;
	}
			
	function edit($id,$form)
	{
		if($this->event->update($id, $form)){
			$data['error']= false;
			$data['msg']= $form['event_name'].' Successfully Updated..';
		}
		else{
			$data['error']= true;
			$data['msg']  = 'Data Base Error <br> ERROR: prvEvn/upd/101';
		}
			
		return $data;
	}	
			
	public function save()
	{
		$this->form_validation->set_message('is_unique', "The<b> %s </b>already Exist!");
		$this->form_validation->set_message('is_unique2', "The<b> %s </b>already Exist!");
		
		//check if it new record or update to set the validation rules
		$id = $this->input->post('id');
		//insert
		if(empty($id) && $id=='')
		{
		$this->form_validation->set_rules('event_name'	, 'Event Name' , 'trim|required|min_length[3]|max_length[255]|is_unique[previous_event.event_name]');
		}
		
		//update
		if($id && !empty($id))
		{
		$this->form_validation->set_rules('event_name'	, 'Event Name', 'trim|required|min_length[3]|max_length[255]|is_unique2[previous_event.event_name.id.'.$id.']');
		}
				
		$this->form_validation->set_rules('country'			, 'Country'					, 'trim|required');
		$this->form_validation->set_rules('city'			, 'City'					, 'trim|required');
		$this->form_validation->set_rules('address'			, 'Address'					, 'trim|required');
		$this->form_validation->set_rules('event_subject'	, 'Event Subject'			, 'trim|required');
		$this->form_validation->set_rules('seasonal'		, 'Seasonal'				, 'trim|required');
		$this->form_validation->set_rules('event_start'		, 'Event Start'				, 'trim|required');
		$this->form_validation->set_rules('event_end'		, 'Event End'				, 'trim|required');
		$this->form_validation->set_rules('company_id'		, 'Company|Organisers'		, 'trim|required');
		$this->form_validation->set_rules('contact_id'		, 'Organiser Responsable'	, 'trim|required');
		$this->form_validation->set_rules('notes'			, 'Notes'					, 'trim|required');
		$this->form_validation->set_rules('attendance'		, 'Attendance'				, 'trim|required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();			
			die(json_encode($data));
			//die(json_encode($errors));
		}
		else
		{
			$form['event_name'] 	= ucwords(strtolower($this->input->post('event_name')));
			$form['country'] 		= $this->input->post('country');
			$form['city'] 			= $this->input->post('city');
			$form['address'] 		= $this->input->post('address');
			$form['event_subject'] 	= $this->input->post('event_subject');
			$form['seasonal'] 		= ucwords(strtolower($this->input->post('seasonal')));
			$form['event_start'] 	= $this->input->post('event_start');
			$form['event_end'] 		= $this->input->post('event_end');
			$form['company_id'] 	= $this->input->post('company_id');
			$form['contact_id'] 	= $this->input->post('contact_id');
			$form['notes'] 			= $this->input->post('notes');
			$form['attendance'] 	= $this->input->post('attendance');
			//NEW/Insert
			if($this->input->post('id')=='')
			{
				$data = $this->create($form);
				$this->log('Previous Event', 'Create record with ID: '.$data['id'].' : '.$this->session->userdata['ip_address']);
			}
			//Edit/Update
			if($this->input->post('id'))
			{
				$id = $this->input->post('id');
				$data = $this->edit($id, $form);
				$this->log('Previous Event', 'Updated record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
			}
			die(json_encode($data,true));
		}
	}

}
